﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebNavigationApi.Models;
using WebNavigationApi.Models.ViewModel;
using WebNavigationApi.Utils;

namespace WebNavigationApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private WNContext _context;
        private static ArrayList VerifyCodes = new ArrayList();
        public UserController(WNContext context)
        {
            _context = context;
        }

        [Route("GetToken")]
        [HttpGet]
        public IActionResult GetToken(string username,string password,string verifyCode)
        {
            
            var user = _context.WN_USER
                .Where(p => p.USERNAME == username && p.PASSWORD == password).FirstOrDefault();
            if (!CheckVerifyCode(verifyCode))
            {
                return Ok(new Result(true, 900, "验证码错误", ""));
            }

            if (user == null)
            {
                return Ok(new Result(true, 900, "用户名或密码错误", ""));
            }
            var tokenObj = new TokenModelJwt()
            {
                userId = user?.ID,
                userName = user?.USERNAME,
            };
            var token = JwtHelper.IssueJwt(tokenObj);
            return Ok(new Result(true,200,"", token) );
        }


        [Route("GetUserInfo")]
        [HttpGet]
        public IActionResult GetUserInfo(string token)
        {
            token = token.Replace("Bearer ","");
            var tokenModelJwt = JwtHelper.SerializeJwt(token);
            var user = _context.WN_USER.Where(p => p.ID == tokenModelJwt.userId).Select(p => new
            {
                username = p.USERNAME,
                userid = p.ID
            });
            return Ok(new Result(true, 200, "", user));

        }


        [Route("RegisterUser")]
        [HttpPost]
        public IActionResult RegisterUser(UserRequest request)
        {
            var user = new WN_USER()
            {
                ID = Guid.NewGuid().ToString("N"),
                IMG = request.image,
                USERNAME = request.username,
                PASSWORD = request.password,
                CREATETIME = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"),
            };
            _context.Add(user);
            _context.SaveChanges();

            return Ok(new Result(true, 200, "保存成功",""));
        }

        [Route("GetVerifyCode")]
        [HttpGet]
        public IActionResult GetVerifyCode()
        {
            var code = "";
            byte[] imageBytes = VerifyCode.Create(out code).ToArray();
            var VerifyCodeLength = 100;
            if (VerifyCodes.Count>= VerifyCodeLength)
            {
                VerifyCodes.Insert(0, code.ToLower()); 
            }

            VerifyCodes.Add(code.ToLower());

            string base64String = Convert.ToBase64String(imageBytes);

            return Ok(new Result(true, 200, "", base64String));
        }

       
        private bool CheckVerifyCode(string code)
        {
            if (VerifyCodes.IndexOf(code.ToLower()) >= 0)
            {
                return true;
            }
            return false;

        }

    }
}
